{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# python 非递归方法，常数空间，从顶到下，逐层连接\n",
    "class Solution:\n",
    "    def connect(self, root: 'Node') -> 'Node':\n",
    "        left_most = root # left_most表示当前层的最左边节点\n",
    "        while(left_most): # 每次循环连接当前层的下一层\n",
    "            head = tail = Node(0) # head表示下一层的虚拟头部\n",
    "            curr = left_most\n",
    "            while(curr): # 遍历当前层，将下一层连接\n",
    "                if(curr.left):\n",
    "                    tail.next = curr.left\n",
    "                    tail = tail.next\n",
    "                if(curr.right):\n",
    "                    tail.next = curr.right\n",
    "                    tail = tail.next\n",
    "                curr = curr.next\n",
    "            left_most = head.next\n",
    "        return root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "# Definition for a Node.\n",
    "class Node:\n",
    "    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):\n",
    "        self.val = val\n",
    "        self.left = left\n",
    "        self.right = right\n",
    "        self.next = next\n",
    "\"\"\"\n",
    "# 非递归方法，从顶到下，逐层连接\n",
    "class Solution:\n",
    "    def connect(self, root: 'Node') -> 'Node':\n",
    "        left_most = root # left_most表示当前层的最左边节点\n",
    "        while(left_most): # 每次循环连接当前层的下一层\n",
    "            curr = left_most\n",
    "            left_most = None\n",
    "            while(curr and not curr.left and not curr.right): # 找到第一个有子节点的节点\n",
    "                curr = curr.next\n",
    "            if(curr):\n",
    "                if(curr.left):\n",
    "                    left_most = curr.left\n",
    "                    curr.left.next = curr.right\n",
    "                else:\n",
    "                    left_most = curr.right\n",
    "                temp = curr.next\n",
    "                while(temp):\n",
    "                    # 找到下一个有子节点的节点\n",
    "                    while(temp and temp.left == None and temp.right == None):\n",
    "                        temp = temp.next\n",
    "                    #将curr与temp的子节点相连\n",
    "                    if(temp):\n",
    "                        if(curr.right):\n",
    "                            curr.right.next = temp.left if temp.left else temp.right\n",
    "                        else:\n",
    "                            curr.left.next = temp.left if temp.left else temp.right\n",
    "                        if(temp.left):\n",
    "                            temp.left.next = temp.right\n",
    "                        curr = temp\n",
    "                        temp = curr.next\n",
    "        return root"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'null' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-ff8c6b9ad5b1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m7\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m8\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m9\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mnull\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m6\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'null' is not defined"
     ]
    }
   ],
   "source": [
    "[1,2,3,4,5,null,6,7,null,null,null,null,8]\n",
    "[1,null,-9,null,8,4,-3,null,null,-3,null,-6,null,null,-6,-4,-9,null,null,6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "x = 5 \n",
    "x = 4 if 4 <5 else 7\n",
    "x = np.where(3<4, 5,6)\n",
    "print(x)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
